# -*- coding: utf-8 -*-
# @Author: Zach-Pineappleman
# @E-mail: 354399824@qq.com
# @Date:   2025-03-13 21:22:04
# @File_name:audio_player.py
# @Last Modified by:   Zach-Pineappleman
# @Last Modified time: 2025-03-13 21:22:23

# audio_player.py
# 音频播放
import threading
import time
from pydub import AudioSegment
from pydub.playback import play
from config import *

class AudioPlayer:
    def __init__(self):
        self.playing = False
        self.play_thread = None

    def play_audio(self, file_path):
        self.playing = True
        sound = AudioSegment.from_file(file_path)
        play(sound)
        self.playing = False

    def play_text(self, text):
        from dashscope.audio.tts_v2 import SpeechSynthesizer
        synthesizer = SpeechSynthesizer(model=TTS_MODEL, voice=TTS_VOICE)
        audio = synthesizer.call(text)
        if audio:
            with open("output.mp3", "wb") as f:
                f.write(audio)
            self.play_audio("output.mp3")